package com.winning.cqs.pregnantbabycare_cqs_v1.table;

import java.io.Serializable;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.winning.cqs.pregnantbabycare_cqs_v1.entity.FriendTalksComment;
import com.winning.cqs.pregnantbabycare_cqs_v1.utils.Constants;

public class Comment implements Serializable {

	private static final long serialVersionUID = 1L;

	public String comment_name;

	public String comment_content;

	public String user_id;

	public String replay_id;

	public String is_replay = "0";

	public String talk_id;

	public String comment_date;

	public String replay_name;

	public static void saveCommentIntoDB(ContentResolver cr,
			FriendTalksComment comment) {
		if (null == cr || null == comment) {
			return;
		}

		ContentValues values = new ContentValues();

		values.put(CommentTable.COMMENT_CONTENT, comment.comment_content);
		values.put(CommentTable.COMMENT_NAME, comment.comment_name);
		values.put(CommentTable.COMMENT_DATE, comment.comment_date);
		values.put(CommentTable.IS_REPLAY, comment.is_replay);
		values.put(CommentTable.TALK_ID, comment.talk_id);
		values.put(CommentTable.USER_ID, comment.user_id);
		if (comment.is_replay.equals("1")) {
			values.put(CommentTable.REPLAY_NAME, comment.replay_name);
			values.put(CommentTable.REPLAY_ID, comment.replay_id);
		}
		if (values.size() > 0) {
			cr.insert(CommentTable.CONTENT_URI, values);

		}
	}

	public static ArrayList<FriendTalksComment> queryCommentsByTalkId(
			ContentResolver cr, String talk_id) {
		ArrayList<FriendTalksComment> comments = new ArrayList<FriendTalksComment>();
		String whereStr = CommentTable.TALK_ID + " = " + talk_id;
		if (cr != null) {
			Cursor cursor = cr.query(CommentTable.CONTENT_URI, null, whereStr,
					null, "comment_date asc");
			if (null != cursor && cursor.getCount() > 0) {
				int count = cursor.getCount();
				cursor.moveToFirst();
				for (int k = 0; k < count; k++) {
					FriendTalksComment comment = new FriendTalksComment();
					comment.comment_content = cursor.getString(cursor
							.getColumnIndex(CommentTable.COMMENT_CONTENT));
					comment.comment_date = cursor.getString(cursor
							.getColumnIndex(CommentTable.COMMENT_DATE));
					comment.comment_name = cursor.getString(cursor
							.getColumnIndex(CommentTable.COMMENT_NAME));
					comment.talk_id = cursor.getString(cursor
							.getColumnIndex(CommentTable.TALK_ID));
					comment.user_id = cursor.getString(cursor
							.getColumnIndex(CommentTable.USER_ID));
					comment.is_replay = cursor.getString(cursor
							.getColumnIndex(CommentTable.IS_REPLAY));
					if (comment.is_replay.equals("1")) {
						comment.replay_id = cursor.getString(cursor
								.getColumnIndex(CommentTable.REPLAY_ID));
						comment.replay_name = cursor.getString(cursor
								.getColumnIndex(CommentTable.REPLAY_NAME));
					}
					comments.add(comment);
					cursor.moveToNext();
				}
				cursor.close();
			}
		}

		return comments;
	}

	public static class CommentTable implements BaseColumns {

		public static final String TABLE_NAME = "comment";

		public static final Uri CONTENT_URI = Uri.parse("content://"
				+ Constants.AUTHORITY + "/" + TABLE_NAME);

		public static final String CONTENT_TYPE = "vnd.android.cursor.dir/"
				+ Constants.AUTHORITY + "/" + TABLE_NAME;

		public static final String USER_ID = "user_id";

		public static final String COMMENT_NAME = "comment_name";

		public static final String COMMENT_CONTENT = "comment_content";

		public static final String REPLAY_ID = "replay_id";

		public static final String REPLAY_NAME = "replay_name";

		public static final String IS_REPLAY = "is_replay";

		public static final String TALK_ID = "talk_id";

		public static final String COMMENT_DATE = "comment_date";

		public static final String TABLE_CREATESQL = "CREATE TABLE "
				+ TABLE_NAME + " (" + "_id"
				+ " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + USER_ID
				+ " TEXT," + COMMENT_NAME + " TEXT," + COMMENT_CONTENT
				+ " TEXT," + COMMENT_DATE + " TEXT," + REPLAY_ID + " TEXT,"
				+ REPLAY_NAME + " TEXT," + IS_REPLAY + " boolean," + TALK_ID
				+ " TEXT" + ");";

	}

}
